﻿using System;
using System.Runtime.InteropServices;

namespace Sliver.Core
{
    public class SliverEngineBridge
    {
        public const string SliverDll = "SliverDll";
        
        [DllImport(SliverDll)]
        public static extern MemoryInfo SystemInit(string configPath);

        [DllImport(SliverDll)]
        public static extern int SystemSetDebugFunction(IntPtr infoFunction, IntPtr warnFunction, IntPtr errorFunction);

        [StructLayout(LayoutKind.Sequential)]
        public struct MemoryInfo
        {
            public IntPtr running_buffer;
            public IntPtr removed_buffer;
            // 每个车道上有几辆车
            public IntPtr lane_vehicle_count;
            public int intersection_count;
            public IntPtr raw_intersections;
            public int lane_count;
            public IntPtr raw_lanes;
        }

        [DllImport(SliverDll, CallingConvention = CallingConvention.Cdecl)]
        public static extern int SystemSetMemoryBuffer(MemoryInfo memoryInfo);

        [DllImport(SliverDll)]
        public static extern void SetLightPhaseChangeListener(IntPtr callback);

        [DllImport(SliverDll)]
        public static extern int SystemRelease();

        [StructLayout(LayoutKind.Sequential)]
        public struct VehiclesUpdateInfo
        {
            public int running_count;
            public int remove_count;
        }

        [DllImport(SliverDll)]
        public static extern VehiclesUpdateInfo NextStep();

        [DllImport(SliverDll)]
        public static extern int GetVehicleCount();

        [DllImport(SliverDll)]
        public static extern void UpdateLaneInfo();

        [DllImport(SliverDll)]
        public static extern void UpdateLaneWaitingVehicleCount();

        [DllImport(SliverDll)]
        public static extern void SetVehicleInfo(VehicleRawInfo rawInfoInfo);

        [DllImport(SliverDll)]
        public static extern float GetAverageTravelTime();

        [DllImport(SliverDll)]
        public static extern void SetRandomSeed(int seed);

        [DllImport(SliverDll)]
        public static extern void GetCrossPoints();

        [DllImport("kernel32", SetLastError = true)]
        public static extern void FreeLibrary(IntPtr module);
    }
}
